package org.example.camundaapp.controller;

import com.alibaba.fastjson2.JSON;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.history.HistoricTaskInstance;
import org.example.camundaapp.service.CamundaHistoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 历史数据查询模块
 * @className: HistoryController
 * @author: ZHRJ
 * @date: 2025-07-04 11:31
 * @Version: 1.0
 * @description:
 */
@Slf4j
@RestController
@RequestMapping("/api/history")
public class HistoryController {
    private final CamundaHistoryService historyService;
    @Autowired
    public HistoryController(CamundaHistoryService historyService) {
        this.historyService = historyService;
    }


    /**
     * 审批历史查询
     * GET /api/history/task?processInstanceId={id}
     * @param processInstanceId 流程实例ID
     * @return
     */
    @GetMapping("/task")
    public ResponseEntity<List<HistoricTaskInstance>> getTaskHistory(
            @RequestParam String processInstanceId) {
        List<HistoricTaskInstance> tasks = historyService.getTaskHistory(processInstanceId);

        return ResponseEntity.ok(tasks);
    }


    /**
     * 获取流程历史
     * @param processInstanceId 流程实例id
     * @return 历史列表
     */
    @GetMapping("/processHistory/{processInstanceId}")
    public ResponseEntity<List<HistoricActivityInstance>> getProcessHistory(@PathVariable String processInstanceId) {
        List<HistoricActivityInstance> history = historyService.getProcessHistory(processInstanceId);
        log.info("获取流程历史，流程实例ID：{}，历史列表：{}", processInstanceId, JSON.toJSONString(history));
        return ResponseEntity.ok(history);
    }
}
